import { Component, inject, OnInit, ViewChild } from '@angular/core';
import { STPage, STComponent, STColumn, STChange, STModule } from '@delon/abc/st';
import { DelonFormModule, SFSchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzContextMenuService } from 'ng-zorro-antd/dropdown';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzSpinModule } from 'ng-zorro-antd/spin';
import { Api } from 'src/app/shared/api/api';
import { ResponseCode } from 'src/app/shared/response.code';
@Component({
  selector: 'app-system-role-user',
  templateUrl: './role-user.component.html',
  providers: [{ provide: NzContextMenuService }],
  imports: [NzSpinModule, DelonFormModule, NzButtonModule, NzCardModule, STModule]
})
export class SystemRoleUserComponent implements OnInit {
  record: NzSafeAny = {};
  params: NzSafeAny = {};
  searchParams: NzSafeAny = {};
  page: NzSafeAny = {
    records: [],
    current: 1,
    total: 0,
    size: 10
  };
  pagination: STPage = {
    front: false,
    pageSizes: [10, 20, 30, 40, 50],
    total: true,
    showSize: true,
    showQuickJumper: true
  };

  searchSchema: SFSchema = {
    properties: {
      username: {
        type: 'string',
        title: '用户名'
      }
    }
  };

  @ViewChild('st', { static: true }) st!: STComponent;
  columns: STColumn[] = [
    { title: '序号', type: 'no', width: '50px' },
    { title: '名称', index: 'username' }
  ];
  protected readonly http = inject(_HttpClient);
  protected readonly modal = inject(NzModalRef);

  ngOnInit() {
    this.query();
  }

  change(e: STChange) {
    if (e.type === 'pi' || e.type === 'ps') {
      this.params.size = e.ps;
      this.params.current = e.pi;
      this.query();
    }
  }

  search(event: NzSafeAny) {
    this.searchParams = event || {};
    this.query();
  }

  query() {
    const current: number = this.params.current || 1;
    const size: number = this.params.size || 10;
    this.searchParams.roleId = this.record.id;
    this.http.get(`${Api.BaseRoleApi}/page/user/${current}/${size}`, this.searchParams).subscribe((res: NzSafeAny) => {
      if (res && res.code === ResponseCode.SUCCESS) {
        if (res.data) this.page = res.data;
      }
    });
  }

  close() {
    this.modal.destroy();
  }
}
